package com.tmc.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.entity.common.EthClient;
import com.tmc.service.baseinfo.EthClientServiceImpl;
import com.tmc.web.BaseController;

@Controller
@RequestMapping("/client")
public class ClientController extends BaseController{
	
	@Autowired
	private EthClientServiceImpl ethClientService;
	
	/**
	 * 客户列表
	 * 
	 * @param cond
	 * @param pageSize
	 * @param pageIndex
	 * @return
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(EthClient cond ,Integer pageSize ,Integer pageIndex, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		if (pageIndex == null || pageIndex < 1)
			pageIndex = 1;
		if (pageSize == null || pageIndex < 1 || pageIndex > 1000)
			pageSize = 10;
		Map<String, Object> map = new HashMap<String, Object>();
		Pagination<EthClient> page = ethClientService.findByCon(context, cond, pageIndex, pageSize);
		map.put("page", page);
		map.put("ethClient", cond);
		map.put("context", context);
		return new ModelAndView("views/client/client-list",map);
	}
	
	/**
	 * 修改页面跳转
	 * 
	 * @param clientId
	 * @return
	 */
	@RequestMapping(value="/editPage")	
	public ModelAndView editPage(Long clientId, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		EthClient client = null;
		if(clientId != null){
			client = ethClientService.getById(context, clientId);
		} else {
			client = new EthClient();
		}
		map.put("client", client);
		map.put("context", context);
		return new ModelAndView("views/client/client-edit", map);
	}
	
	/**
	 * 保存客户
	 * 
	 * @param client
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save")
	@ResponseBody
	public String save(EthClient client ,HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		CommonResult result = ethClientService.save(context, client);
		return result.toJson();		
	}
	
	@RequestMapping(value="/search")
	@ResponseBody
	public String search(String query){
		List<EthClient> list = ethClientService.search(query);
		JSONArray array = new JSONArray();
		for(EthClient client : list){
			//"Key":"clientId","Name":"张建华"
			JSONObject obj = new JSONObject();
			obj.put("Key", client.getClientId() 
					+ "");
			obj.put("Name",client.getClientName());
			array.add(obj);
		}
 		return array.toJSONString();	
	}
}
